package com.asiainfo.heshanghui.activity;

import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.asiainfo.heshanghui.configs.ConstantsME;
import com.asiainfo.heshanghui.configs.InterfaceConfig;
import com.asiainfo.heshanghui.networkModel.HttpUtil;
import com.framework.Utils.PreferencesHelper;
import com.framework.Utils.ToastUtil;
import com.framework.Utils.Utils;
import com.framework.security.RSAmethod;
import com.framework2.base.BaseOnClickListener;
import com.framework2.base.BiuooBaseActivity;
import com.framework2.customview.TitleView;
import com.framework2.utils.BiuooCustomProgressDialog;
import com.framework2.utils.BiuooCustomProgressDialogUtils;
import com.framework2.utils.ConstantRequestUtil;
import com.asiainfo.heshanghui.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class BiuooHomepageActivity extends BiuooBaseActivity implements ActivityCompat.OnRequestPermissionsResultCallback {

    @BindView(R.id.btn1)
    Button btn1;
    @BindView(R.id.btn2)
    Button btn2;
    @BindView(R.id.btn3)
    Button btn3;
    @BindView(R.id.btn4)
    Button btn4;
    private long exitTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.setTranslucentStatus(this, true);
        Utils.setStatusBarTintDrawable(this, R.mipmap.biuoo_bg_top);
        setContentView(R.layout.biuoo_activity_main);
        ButterKnife.bind(this);
        initView();
    }


    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.leftLayout:
                BiuooCustomProgressDialogUtils.showProcessDialog(this, "正在识别");
                Utils.h.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        BiuooCustomProgressDialogUtils.dismissProcessDialog();
                        ToastUtil.getInstance().showToast("请手动输入号码!");
                    }
                }, 2000);
                break;
            case R.id.rightLayout:
                ToastUtil.getInstance().showToast("未添加扫码功能，请手动输入!");
//                BiuooCustomProgressDialogUtils.showProcessDialog(this, "跳转扫码");
//                Utils.h.postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        BiuooCustomProgressDialogUtils.dismissProcessDialog();
//                        ToastUtil.getInstance().showToast("未添加扫码功能，请手动输入!");
//                    }
//                }, 2000);
                break;
            case R.id.tv_input:
                startActivity(BiuooInputPhoneActivity.class);
                break;
            default:
                break;
        }
    }


    private void initView() {
        TitleView titleView = (TitleView) findViewById(R.id.titleView);
        titleView.setTitle("导购").setLeftOnClickListener(new BaseOnClickListener() {
            @Override
            protected void onBaseClick(View v) {
                finish();
            }
        });
    }


    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (System.currentTimeMillis() - this.exitTime > 2000L) {
                Toast.makeText(this.getApplicationContext(), this.getResources().getString(R.string.biuoo_exist) + this.getResources().getString(R.string.app_name), Toast.LENGTH_SHORT).show();
                this.exitTime = System.currentTimeMillis();
            } else {
                // 返回主界面
                /**
                 * 退出登录,清空数据
                 */
                finish();
//                int pid = android.os.Process.myPid();
//                android.os.Process.killProcess(pid);
            }
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    @OnClick({R.id.btn1, R.id.btn2, R.id.btn3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn1:
                startActivity(ScanActivity.class);
                break;
            case R.id.btn2:
                startActivity(PutInStoreActivity.class);
                break;
            case R.id.btn3:
                startActivity(BiuooInputPhoneActivity.class);
                break;
        }
    }

    @OnClick(R.id.btn4)
    public void onViewClicked() {
        BiuooCustomProgressDialogUtils.showProcessDialog(this, "模拟登录");
        HttpUtil.getInstance().requestLogin(InterfaceConfig.login, "18725618945", "qqqqqq", new HttpUtil.OnRequestResult<String>() {
            @Override
            public void onSuccess(String... msg) {
                BiuooCustomProgressDialogUtils.dismissProcessDialog();
                PreferencesHelper.getInstance().putInfo(ConstantsME.PHONE, RSAmethod.rsaEncrypt(BiuooHomepageActivity.this, "18725618945"));
                PreferencesHelper.getInstance().putInfo(ConstantsME.token, msg != null ? msg[0] : "");
                PreferencesHelper.getInstance().putInfo(ConstantsME.nick, msg != null ? msg[1] : "");
                PreferencesHelper.getInstance().putInfo(ConstantsME.imgUrl, msg != null ? msg[2] : "");
                PreferencesHelper.getInstance().putInfo(ConstantsME.LOGINED, true);
                try {
                    Thread.sleep(200);
                    ConstantRequestUtil.getInstance().requestJustShopIds(BiuooHomepageActivity.this);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFail(int code, String msg) {
                BiuooCustomProgressDialogUtils.dismissProcessDialog();
            }
        });
    }
}
